#include "capturewindow.h"
#include "ui_capturewindow.h"
#include <QPushButton>
#include <QFile>
#include <QPixmap>

capturewindow::capturewindow(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::captureWindow)
    ,capturePacket(new CapturePacket(this))
{
    ui->setupUi(this);


    //scroll bar
    scrollArea = new QScrollArea(this);
    scrollContent = new QWidget(scrollArea);
    scrollLayout = new QVBoxLayout(scrollContent);

    scrollArea->setWidgetResizable(true);
    scrollArea->setWidget(scrollContent);
    scrollArea->setGeometry(540,100,400,600);
    scrollArea->setStyleSheet("background-color: transparent; border: none;");

    //scroll member
    scrollContent->setStyleSheet("background-color: rgba(0, 0, 0, 150);");
    scrollLayout->setAlignment(Qt::AlignTop);
    scrollLayout->setSpacing(10);
    scrollLayout->setContentsMargins(5,5,5,5);

    connect(capturePacket,&CapturePacket::interfacesFound,this,&capturewindow::updateInterfaceList);
    connect(ui->interfaceButton,&QPushButton::clicked,[this](){
        capturePacket->showDeviceName();
    });
    connect(ui->clearButton,&QPushButton::clicked,this,&capturewindow::clearInterfaceList);

}

capturewindow::~capturewindow()
{
    delete ui;
    qDeleteAll(interfaceButtons);
}

void capturewindow::clearInterfaceList(){
    //clear buttons
    qDeleteAll(interfaceButtons);
    interfaceButtons.clear();

    //clear layout
    QLayoutItem* item;
    while ((item = scrollLayout->takeAt(0))) {
        delete item->widget();
        delete item;
    }
}

void capturewindow::showCapturePage(const QString& interfaceName){
    capturePage = new CapturePage(interfaceName);
    capturePage->setAttribute(Qt::WA_DeleteOnClose);

    connect(capturePage,&CapturePage::requestBack,this,[this](){
        this->show();
    });

    this->hide();
    capturePage->show();
}

void capturewindow::updateInterfaceList(const QStringList &interfaces){
    //clear buttons
    qDeleteAll(interfaceButtons);
    interfaceButtons.clear();
    //clear layout
    QLayoutItem* item;
    while ((item = scrollLayout->takeAt(0))) {
        delete item->widget();
        delete item;
    }

    for(const QString&ifname: interfaces){
        QPushButton *btn = new QPushButton(ifname,scrollContent);

        btn->setStyleSheet(
            "QPushButton {"
            "   color: rgb(135, 96, 16);"
            "   background-color: rgb(0, 0, 0);"
            "   border: 2px solid rgb(135, 96, 16);"
            "   border-radius: 10px;"
            "   padding: 10px;"
            "   min-width: 250px;"
            "   min-height: 50px;"
            "}"
            "QPushButton:hover {"
            "   background-color: rgb(30, 30, 30);"
            "}"
            );

        //todo connect clicked

        connect(btn,&QPushButton::clicked,[this,ifname](){
            //emit interfaceSelected(ifname);
            showCapturePage(ifname);
        });

        scrollLayout->addWidget(btn);
        interfaceButtons.append(btn);
    }

    scrollLayout->addStretch();
}


